/**
 * @Author wangyl
 * @E-mail wangyl0629@foxmail.com
 **/
package com.wyl.xue.admin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.xue.admin.system.mybatis.entity.SystemMenu;
import com.wyl.xue.admin.system.mybatis.service.SystemMenuService;
import com.wyl.xue.admin.system.mybatis.service.SystemRoleMenuService;
import com.wyl.xue.admin.system.service.Menu;
import com.wyl.xue.admin.system.vo.MenuInfoVo;
import com.wyl.xue.admin.system.vo.MenuTree;
import com.wyl.xue.core.util.UserTools;
import com.wyl.xue.core.util.exception.BizException;
import com.wyl.xue.core.util.result.ResultCode;
import com.wyl.xue.core.util.tree.TreeUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName: MenuImpl
 * @Function: TODO
 * @Date: 2020/11/8 15:41
 * @author wyl
 * @version V1.0
 */
@AllArgsConstructor
@Service
@Slf4j
public class MenuImpl implements Menu {

    final BeanCopier copier = BeanCopier.create(MenuInfoVo.class, SystemMenu.class, false);
    final SystemMenuService systemMenuService;
    final SystemRoleMenuService systemRoleMenuService;

    /**
     * @Description 添加菜单
     * @param menuInfoVo
     * @return void
     * @Date 2020/11/9 22:41
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addMenu(MenuInfoVo menuInfoVo) {
        SystemMenu systemMenu = new SystemMenu();
        copier.copy(menuInfoVo, systemMenu, null);
        systemMenu.setOperator(UserTools.tools.getUserNameByRequest());
        systemMenuService.save(systemMenu);
    }

    /**
     * @Description 删除菜单
     * @param id
     * @return void
     * @Date 2020/11/9 22:41
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMenu(long id) {
        if (systemMenuService.haschildMenu(id)) {
            throw new BizException(ResultCode.HAVERESOURCES);
        }
        systemMenuService.removeById(id);
        systemRoleMenuService.deleteRoleMenusByMenuId(id);
    }

    /**
     * @Description 通过父节点获取菜单树
     * @param parenId
     * @return java.util.List<com.wyl.xue.admin.system.vo.MenuTree>
     * @Date 2020/11/9 22:41
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public List<MenuTree> getMenuTree(long parenId) {
        List<SystemMenu> systemMenuList = systemMenuService.list();
        List<MenuTree> menuTrees = systemMenuList.parallelStream()
                                                 .map(systemMenu -> new MenuTree(systemMenu.getMenuId(), systemMenu.getParentId(), systemMenu.getMenuName()))
                                                 .collect(Collectors.toList());
        return TreeUtil.bulid(menuTrees, -1L);
    }

    /**
     * @Description 分页获取指定父节点下的菜单信息
     * @param parentId
     * @param size
     * @param page
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.wyl.xue.admin.system.mybatis.entity.SystemMenu>
     * @Date 2020/11/9 22:42
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public Page<SystemMenu> getSubmenuByParentIdForPage(long parentId, int page, int size) {
        Page<SystemMenu> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<SystemMenu> systemMenuLambdaQueryWrapper = Wrappers.<SystemMenu>lambdaQuery().eq(SystemMenu::getParentId, parentId);
        return systemMenuService.page(pageInfo, systemMenuLambdaQueryWrapper);
    }

    /**
     * @Description 获取排除功能外的菜单
     * @param parenId
     * @return java.util.List<com.wyl.xue.admin.system.vo.MenuTree>
     * @Date 2020/11/9 22:42
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public List<MenuTree> getMenuTreeExcludeFunctionMenu(long parenId) {
        List<SystemMenu> systemMenuList = systemMenuService.list(Wrappers.<SystemMenu>lambdaQuery().ne(SystemMenu::getType, 2));
        List<MenuTree> menuTrees = systemMenuList.parallelStream()
                                                 .map(systemMenu -> new MenuTree(systemMenu.getMenuId(), systemMenu.getParentId(), systemMenu.getMenuName()))
                                                 .collect(Collectors.toList());
        return TreeUtil.bulid(menuTrees, -1L);
    }

    /**
     * @Description 更新菜单
     * @param systemMenu
     * @return void
     * @Date 2020/11/9 22:47
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public void updateMenu(SystemMenu systemMenu) {
        systemMenu.setOperator(UserTools.tools.getUserNameByRequest());
        systemMenuService.updateById(systemMenu);
    }

    /**
     * @Description 搜索菜单
     * @param menuInfoVo
     * @return java.util.List<com.wyl.xue.admin.system.mybatis.entity.SystemMenu>
     * @Date 2020/11/19 17:35
     * @Author wangyl
     * @Version V1.0
     */
    @Override
    public List<SystemMenu> searchMenu(MenuInfoVo menuInfoVo) {
        LambdaQueryWrapper<SystemMenu> menuLambdaQueryWrapper = Wrappers.lambdaQuery(SystemMenu.class)
                                                                        .like(!Objects.isNull(menuInfoVo.getMenuName()), SystemMenu::getMenuName, menuInfoVo.getMenuName())
                                                                        .like(!Objects.isNull(menuInfoVo.getType()), SystemMenu::getType, menuInfoVo.getType())
                                                                        .like(!Objects.isNull(menuInfoVo.getPerms()), SystemMenu::getPerms, menuInfoVo.getPerms());
        return systemMenuService.list(menuLambdaQueryWrapper);
    }

}
